<style xmlns="http://www.w3.org/1999/html">
    .layui-form-label.required:before { /* 这种写法也可以*/
        content: ' *';
        color: red;
    }

    .open-form .layui-form-label {
        width: 110px;
    }

    .open-form .layui-input-block {
        margin-left: 140px;
    }
</style>
<script type="text/javascript" src="../../../admin/assets/js/pdfobject.min.js"></script>

<!-- 质检表格操作列 -->
<script type="text/html" id="testItemTableBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delTestItem">删除</a>
</script>

<!-- 表单弹窗 -->
<form lay-filter="productReviewForm" class="layui-form productReviewForm open-form">
    <div class="open-model">
        <div class="open-auto">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>客户信息产品信息</legend>
            </fieldset>
            <div class="layui-form-item layui-row">
                <div class="layui-col-md3">
                    <label class="layui-form-label required">客户名称：</label>
                    <div class="layui-input-block">
                        <select id="customerId" lay-search="" name="customerId" lay-filter="customerId"
                                class="common-select-search">
                        </select>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label required">客户订单：</label>
                    <div class="layui-input-block">
                        <select id="cusProductOrder" name="cusProductOrder" lay-filter="cusProductOrder"
                                class="common-select-search">
                        </select>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label required">客户SKU料号：</label>
                    <div class="layui-input-block">
                        <select id="cusSkuCode" name="cusSkuCode" lay-filter="cusSkuCode"
                                class="common-select-search">
                        </select>
                        <input type="hidden" id="extProductId" name="extProductId"/>
                        <input type="hidden" id="orderProId" name="orderProId"/>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label required">客户SKU名称：</label>
                    <div class="layui-input-block">
                        <input id="cusProductName" name="cusProductName" placeholder="请输入产品名称" type="text"
                               class="layui-input"
                               required autocomplete="off"/>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-col-md3">
                    <label class="layui-form-label">客户规格码：</label>
                    <div class="layui-input-block">
                        <input id="cusProductSpec" name="cusProductSpec" placeholder="请输入规格码" type="text"
                               class="layui-input"
                               required autocomplete="off"/>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label required">计量单位：</label>
                    <div class="layui-input-block">
                        <select id="cusProUnitId" name="cusProUnitId" class="common-select-search"
                                />
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">需求数量：</label>
                    <div class="layui-input-block">
                        <input id="cusProductCount" name="cusProductCount" placeholder="请输需求数量" type="text"
                               class="layui-input"
                               required autocomplete="off"/>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label required">计划交货时间：</label>
                    <div class="layui-input-block">
                        <input type="text" id="planGivenTime" name="planGivenTime" class="layui-input"
                               placeholder="请选择计划交货时间">
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-col-md3">
                    <label class="layui-form-label required">客户产品类别：</label>
                    <div class="layui-input-block">
                        <input type="hidden" id="cusProductCategory" name="cusProductCategory">
                        <input id="cusProductType" name="cusProductType" placeholder="请输产品类别" type="text"
                               class="layui-input"
                               required autocomplete="off"/>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">上传图纸附件：</label>
                    <div class="layui-input-block">
                        <div id="fileUploadBtn" class="layui-btn" style="float: left;margin-left: 3px"><i
                                class="layui-icon">&#xe681;</i>上传文件
                        </div>
                        <br/>
                        <div id="addHeaderPdf" class="companyFormPhotos"></div>
                        <input name="attachmentPdf" id="attachmentPdf" type="hidden" class="layui-input"/>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">包装说明：</label>
                    <div class="layui-input-block">

                        <input type="text" id="remark" name="remark" class="layui-input"
                               placeholder="请选择计划交货时间">
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">工艺说明：</label>
                    <div class="layui-input-block">
                        <textarea name="processDescription" placeholder="工艺说明" class="layui-textarea"
                                  autocomplete="off"/>
                    </div>
                </div>
            </div>
            <fieldset class="layui-elem-field layui-field-title">
                <legend>关联内部产品信息</legend>
            </fieldset>
<!--            <div class="layui-form-item layui-row">-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label required">是否关联料号：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <select id="isUseOrAdd" name="isUseOrAdd" lay-filter="isUseOrAdd"-->
<!--                                class="common-select-search" disabled>-->
<!--                            <option value="">请选择</option>-->
<!--                            <option value="2">关联已有产品</option>-->
<!--                        </select>-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label required">产品类别：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <div id="productTypeName" lay-filter="productTypeName">-->
<!--                        </div>-->
<!--                        <input id="productTypeId" name="productTypeId" type="hidden">-->
<!--                        <input type="hidden" class="layui-input" id="productTypeId2" name="productTypeId2">-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label required">内部SKU号：</label>-->
<!--                    <div class="layui-input-block" id="SkuInfo">-->
<!--                        <div id="d1">-->
<!--                            <input type="text" id="inSkuCode" name="inSkuCode" class="layui-input"-->
<!--                                   placeholder="请输入SKU号">-->
<!--                        </div>-->
<!--                        <div id="d2" style="display: none;">-->
<!--                            <select id="inSkuCodeHave" name="inSkuCodeHave" lay-filter="inSkuCodeHave"-->
<!--                                    class="common-select-search">-->
<!--                            </select>-->
<!--                        </div>-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label required">产品属性：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <select id="assemblyNature" name="assemblyNature" lay-filter="assemblyNature"-->
<!--                                class="common-select-search">-->
<!--                        </select>-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
<!--            <div class="layui-form-item layui-row">-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label required">产品性质：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <select id="productNature" name="productNature" lay-filter="productNature"-->
<!--                                class="common-select-search">-->
<!--                        </select>-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label required">内部SKU名称：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <input type="text" id="inSkuName" name="inSkuName" class="layui-input"-->
<!--                               placeholder="请输入名称">-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label required">内部规格码：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <input type="text" id="inSkuSpec" name="inSkuSpec" class="layui-input"-->
<!--                               placeholder="请输入名称">-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label required">基础单位：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <input type="text" disabled id="inUnit" name="inUnit" class="layui-input">-->
<!--                        <input type="hidden" id="inUnitId" name="inUnitId" class="layui-input">-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
<!--            <div class="layui-form-item layui-row">-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label required">计量方式(主)：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <select id="mainCalWay" name="mainCalWay" readonly="readonly" lay-filter="mainCalWay"-->
<!--                                class="layui-input borderNone"></select>-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label required">包装单位(主)：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <select id="mainUnitId" name="mainUnitId" readonly="readonly" lay-filter="mainUnitId"-->
<!--                                class="layui-input borderNone"></select>-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label required">换算率(主)：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <input type="text" id="mainUnitRate" name="mainUnitRate" class="layui-input">-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label">计量方式(辅)：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <select id="subCalWay" name="subCalWay" readonly="readonly" lay-filter="subCalWay"-->
<!--                                class="layui-input borderNone"></select>-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
<!--            <div class="layui-form-item layui-row">-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label">计量单位(辅)：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <select id="subUnitId" name="subUnitId" readonly="readonly" lay-filter="mainUnitId"-->
<!--                                class="layui-input borderNone"></select>-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label">换算率(辅)：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <input type="text" id="subUnitRate" name="subUnitRate" class="layui-input">-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label">成本核算类型：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <select id="costCheckType" name="costCheckType" lay-filter="costCheckType"-->
<!--                                class="layui-input borderNone"></select>-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label">是否质检：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <select id="checkStatus" name="checkStatus" lay-filter="checkStatus"-->
<!--                                class="layui-input borderNone"></select>-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
<!--            <div class="layui-form-item layui-row">-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label">是否料箱管理：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <select id="boxStatus" name="boxStatus" lay-filter="boxStatus"-->
<!--                                class="layui-input borderNone"></select>-->
<!--                    </div>-->
<!--                </div>-->
<!--                <div class="layui-col-md3">-->
<!--                    <label class="layui-form-label">说明：</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <input type="text" id="inRemark" name="inRemark" placeholder="说明" class="layui-input">-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->

            <div class="layui-form-item layui-row">
                <div class="layui-col-md3">
                    <label class="layui-form-label">是否关联新料号：</label>
                    <div class="layui-input-block">
                        <input name="isUseOrAdd" disabled type="text" placeholder="关联内部物料" class="layui-input"  />
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">产品类别：</label>
                    <div class="layui-input-block">
                        <input name="productTypeName" disabled type="text" class="layui-input"  />
                        <input id="productTypeId" name="productTypeId" type="hidden" value="' + d.proTypeId + '"  />
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">产品性质：</label>
                    <div class="layui-input-block">
                        <input name="productNatureName" disabled type="text" class="layui-input"  />
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">内部SKU号：</label>
                    <div class="layui-input-block" id="SkuInfo">
                        <input id="productDetailId" name="productDetailId" type="hidden" />
                        <input id="inSkuCode" name="inSkuCode" disabled type="text" class="layui-input"  />
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-col-md3">
                    <label class="layui-form-label">内部SKU名称：</label>
                    <div class="layui-input-block">
                        <input type="text" id="inSkuName" disabled name="inSkuName" class="layui-input" />
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label required">产品属性：</label>
                    <div class="layui-input-block">
                        <input name="assemblyNatureName" disabled type="text" class="layui-input"  />
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">内部规格码：</label>
                    <div class="layui-input-block">
                        <input type="text" id="inSkuSpec" disabled name="inSkuSpec" class="layui-input" />
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">基础单位：</label>
                    <div class="layui-input-block">
                        <input name="inUnitName" disabled type="text" class="layui-input"/>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-col-md3">
                    <label class="layui-form-label required">计量方式(主)：</label>
                    <div class="layui-input-block">
                        <input type="text" id="mainCalWayName" disabled name="mainCalWayName" class="layui-input" />
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label required">包装单位(主)：</label>
                    <div class="layui-input-block">
                        <input type="text" id="mainUnitName" disabled name="mainUnitName" class="layui-input" />
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label required">换算率(主)：</label>
                    <div class="layui-input-block">
                        <input type="text" id="mainUnitRate" disabled name="mainUnitRate" class="layui-input" />
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">计量方式(辅)：</label>
                    <div class="layui-input-block">
                        <input type="text" id="subCalWayName" disabled name="subCalWayName" class="layui-input" />
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-col-md3">
                    <label class="layui-form-label">计量单位(辅)：</label>
                    <div class="layui-input-block">
                        <input type="text" id="subUnitName" disabled name="subUnitName" class="layui-input" />
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">换算率(辅)：</label>
                    <div class="layui-input-block">
                        <input type="text" id="subUnitRate" disabled name="subUnitRate" class="layui-input" />
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">成本核算类型：</label>
                    <div class="layui-input-block">
                        <select id="costCheckType" disabled name="costCheckType" lay-filter="costCheckType" class="layui-input borderNone"></select>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">是否质检：</label>
                    <div class="layui-input-block">
                        <select id="checkStatus" disabled name="checkStatus" lay-filter="checkStatus" class="layui-input borderNone"></select>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-col-md3">
                    <label class="layui-form-label">是否料箱管理：</label>
                    <div class="layui-input-block">
                        <select id="boxStatus" disabled name="boxStatus" lay-filter="boxStatus" class="layui-input borderNone"></select>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <label class="layui-form-label">说明：</label>
                    <div class="layui-input-block">
                        <input type="text" id="inRemark" name="inRemark" class="layui-input" />
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-col-md6">
                    <fieldset class="layui-elem-field layui-field-title">
                        <legend>图纸信息：</legend>
                    </fieldset>
                    <div id="TuZhiPdf" style="height: 100%;margin-left:10px">

                    </div>
                </div>
                <div class="layui-col-md6">
                    <fieldset class="layui-elem-field layui-field-title">
                        <legend>成品验收标准：</legend>
                    </fieldset>
                    <div class="layui-form-item layui-row">
                        <button type="button" id="addTestItem" class="layui-btn" lay-filter="addTestItem"
                                lay-submit>添加检测项
                        </button>
                        <table class="layui-table" id="testItemTable" lay-filter="testItemTable"></table>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="productTaskFormSubmit" lay-submit>保存</button>
        </div>
    </div>
</form>

<script type="text/html" id="viewAttachmentHtml">
    <div id="example1" style="height: 100%;"></div>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'laydate', 'admin', 'laytpl', 'upload', 'config'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var config = layui.config;
        var upload = layui.upload;
        var table = layui.table;
        var admin = layui.admin;
        var form = layui.form;
        var laydate = layui.laydate;


        laydate.render({
            elem: '#planGivenTime'
            , type: 'datetime'
        });

        let oldSkuInfo = new Map()

        let customerName = "";
        let postName = "";
        let testItemData = [];

        let fileName = "";

        //初始化主辅计量方式
        admin.getDictList("mainCalWay", "product_unit_cal_way", null, "辅助计量方式");
        admin.getDictList("subCalWay", "product_unit_cal_way", null, "主计量方式");

        admin.getDictList("costCheckType", "product_detail_cost_check_type", null, "成本核算");
        admin.getDictList("checkStatus", "sys_yes_no", null, "是否质检");
        admin.getDictList("boxStatus", "sys_yes_no", null, "是否料箱管理");


        //加载是默认填写
        // document.getElementById("d2").style.display = "none";//隐藏
        // document.getElementById("d1").style.display = "";//显示


        //addTestItem
        $("#addTestItem").click(function () {
            let newRow = {
                name: "",
                standard: "",
                utensil: "",
                way: "",
                importance: "",
                detectionRate: ""
            };
            testItemData.push(newRow);
            table.reload('testItemTable', {
                data: testItemData
            })
        })


        //移除某一行
        table.on('tool(testItemTable)', function (obj) {
            if (obj.event === 'delTestItem') { //删除
                obj.del(); // 删除当前行
                // 获取表格数据
                testItemData = table.cache['testItemTable'];
                //移除空数据
                testItemData = testItemData.filter(item => Object.keys(item).length > 0);
                table.reload('testItemTable', {
                    data: testItemData
                });
            }
        });

        //初始化质检标准表
        table.render({
            elem: '#testItemTable',
            data: testItemData,
            id: 'testItemTable',
            limit: 9999,
            cols: [[
                {type: 'numbers', title: '序号'}
                , {
                    field: 'name',
                    edit: 'text',
                    align: 'center',
                    title: '检查项'
                }
                , {field: 'standard', edit: 'text', sort: true, title: '标准'}
                , {field: 'utensil', edit: 'text', sort: true, title: '器具'}
                , {field: 'way', edit: 'text', sort: true, title: '方法'}
                , {field: 'importance', edit: 'text', sort: true, title: '重要程度'}
                , {field: 'detectionRate', edit: 'text', sort: true, title: '检测率'}
                , {align: 'center', toolbar: '#testItemTableBar', title: '操作', width: 100}
            ]]
        });

        // 上传附件-pdf
        upload.render({
            elem: '#fileUploadBtn'
            , url: config.upload_url + 'uploadFile'
            , data: {}
            , accept: 'file'
            , exts: 'jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx|apk'
            , headers: {Authorization: 'Bearer ' + config.getToken()}
            , done: function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg || "上传成功", {icon: 1});
                    let imagUrl = res.data.url;
                    let name = res.data.fileName;
                    fileName = res.data.fileName;
                    $('#addHeaderPdf').html('<div class="attachments">' +
                        '<div><img name="attachments" src="../../../admin/assets/images/attachmentsIcon/pdf.png"><span class="file-a">' + name + '</span></div></div>');
                    $('#attachmentPdf').val(imagUrl);

                    if (imagUrl && imagUrl.indexOf(".pdf") !== -1) {
                        PDFObject.embed(imagUrl, "#TuZhiPdf");
                    } else {
                        let attachmentHtml = "";
                        imagUrl.split(",").forEach((attachmentUrl, index) => {
                            attachmentHtml += '<div><div><img style="max-width: 685px" src="' + attachmentUrl + '"></div></div>';
                        });
                        $('#TuZhiPdf').html(attachmentHtml);
                    }

                    $(".attachments").click(function () {
                        let json = {};
                        json.attachment = imagUrl;
                        showViewAttachmentModel(json);
                    })
                } else if (res.code == 502) {
                    return location.replace('admin/login.html');
                } else {
                    layer.msg(res.msg || "上传失败", {icon: 2});
                }
            }
        });

        //客户名称的点击事件
        form.on('select(customerId)', function (data) {
            customerName = data.elem.selectedOptions[0].text;
            //查询正式订单
            let types = ["6", "7"];
            $('#cusProductOrder').empty(); // 清空所有选项
            //加载客户订单
            admin.initDataSelect("enterprise/sell/admin/order/allOrderListByCustomer", {
                customerId: data.value,
                types: types
            }, "cusProductOrder", null, "订单", "code", "id");
        });

        //客户订单的选择事件
        form.on('select(cusProductOrder)', function (data) {
            $('#cusSkuCode').empty(); // 清空所有选项
            admin.initDataSelect("enterprise/sell/admin/orderPro/allSkuCodeByOrderId", {orderId: data.value}, "cusSkuCode", null, "sku料号", "customerSkuItem", "id");
        });

        //客户sku物料的选择事件  填充剩余的
        form.on('select(cusSkuCode)', function (data) {
            let id = data.value;
            admin.reqSync('enterprise/sell/admin/orderPro/getOrderProId', {
                cusSkuId: id
            }, res => {
                if (res.code === 200) {
                    if (res.data != null) {
                        $("#cusProductName").val(res.data.customerSkuName);
                        $("#cusProductSpec").val(res.data.customerSpecCode);
                        $("#extProductId").val(res.data.cusProductId);
                        $("#orderProId").val(id);
                        //初始化单位
                        admin.initDataSelect("enterprise/product/admin/productUnit/list", null, "cusProUnitId", res.data.proUnitId, "单位", "name", "id");
                        $("#cusProductCount").val(res.data.quantity);
                        $("#cusProductType").val(res.data.proTypeName);
                        $("#cusProductCategory").val(res.data.proTypeId);
                        if (res.data.productDetail){
                            form.val("productReviewForm",res.data.productDetail)
                            $("#inSkuCode").val(res.data.productDetail.skuCode);
                            $("#inSkuName").val(res.data.productDetail.skuName);
                            $("#inSkuSpec").val(res.data.productDetail.skuSpec);
                            $("#productDetailId").val(res.data.productDetail.id);
                        }
                    } else {
                        $("#cusProductName").val("");
                        $("#cusProductSpec").val("");
                        $("#cusProductCount").val("");
                        $("#cusProductType").val("");
                        $("#extProductId").val("");
                        $("#cusProductCategory").val("");
                    }
                }
            }, 'get');
        });


        //postId
        form.on('select(postId)', function (data) {
            postName = data.elem.selectedOptions[0].text;
        });

        // 已有物料的选择事件
        form.on('select(inSkuCodeHave)', function (data) {
            let val = data.value;
            let skuInfo = {};
            if (oldSkuInfo.get(val)) {
                skuInfo = oldSkuInfo.get(val);
                //回显
                //sku名称
                $('#inSkuName').val(skuInfo.skuName);
                //规格
                $('#inSkuSpec').val(skuInfo.skuSpec);

                $('#productTypeId2').val(skuInfo.productTypeId);

                //产品类别
                admin.initDataSelectTree(true, 'enterprise/product/admin/productType/getAllTreeData', {materialNatureList: [2, 3, 7]}, 'productTypeName', skuInfo.productTypeId, "物料分类", "productTypeId", null);
                //产品性质
                $('#productNature').val(skuInfo.assemblyNature);
                //产品属性
                $('#assemblyNature').val(skuInfo.assemblyNature);

                admin.reqSync('enterprise/product/admin/productUnit/getIsBaseUnitProductUnitByType', {
                    type: 0,
                    productTypeId: skuInfo.productTypeId
                }, res => {
                    if (res.code === 200) {

                        if (res.data != null) {
                            let infoName = res.data.name || '';
                            let infoId = res.data.id || '';
                            $('#inUnit').val(infoName);
                            $('#inUnitId').val(infoId);
                        } else {
                            $('#inUnit').val("");
                            $('#inUnitId').val("");
                        }
                    }
                }, 'get');
                //说明
                $('#inRemark').val(skuInfo.remark);
                form.render("select")
            }
        });

        var productUnitList = [];

        //物料类型
        admin.initDataSelectTree(true, 'enterprise/product/admin/productType/getAllTreeData', {materialNatureList: [2, 3, 7]}, 'productTypeName', null, "物料分类", "productTypeId", function (data) {
            let productTypeId = data.change[0].value;
            $('#inUnit').val("");
            //清空下拉
            $('#inSkuCodeHave').empty();
            admin.initDataSelectAndMap("enterprise/product/admin/productDetailWMS/list", {
                categoryType: "production",
                productTypeId: productTypeId
            }, "inSkuCodeHave", null, "SKU物料", "codeAndName", null, oldSkuInfo);

            //物料列别的改变时间
            admin.req('enterprise/product/admin/productItem/listByType', {productType: productTypeId}, function (res) {
                if (res.code === 200) {
                    testItemData = res.data;
                    //重新渲染data
                    table.reload('testItemTable', {
                        data: testItemData
                    })
                }
            })

            //基础单位
            admin.reqSync('enterprise/product/admin/productUnit/getIsBaseUnitProductUnitByType', {
                type: 0,
                productTypeId: productTypeId
            }, res => {
                if (res.code === 200) {
                    if (res.data != null) {
                        let infoName = res.data.name || '';
                        let infoId = res.data.id || '';
                        $('#inUnit').val(infoName);
                        $('#inUnitId').val(infoId);
                    } else {
                        $('#inUnit').val("");
                        $('#inUnitId').val("");
                    }
                }
            }, 'get');
            //主计量方式的改变事件
            admin.req('enterprise/product/admin/productType/getProductTypeVoById', {productTypeId: productTypeId}, (res) => {
                if (res.code === 200) {
                    //包装单位集合
                    productUnitList = res.data.productUnitList
                }
            });
        });

        form.on('select(mainCalWay)', function (data) {
            $('#mainUnitId').empty();
            if (data.value) {
                productUnitList.forEach(v => {
                    if (v.calWay == data.value) {
                        $('#mainUnitId').append(new Option(v.name, v.id));// 下拉菜单里添加元素
                    }
                })
            }
            form.render("select");
        })

        form.on('select(subCalWay)', function (data) {
            $('#subUnitId').empty();
            if (data.value) {
                productUnitList.forEach(v => {
                    if (v.calWay == data.value) {
                        $('#subUnitId').append(new Option(v.name, v.id));// 下拉菜单里添加元素
                    }
                })
            }
            form.render("select");
        })

        // 查看附件
        function showViewAttachmentModel(data) {
            admin.open({
                type: 1,
                area: ['60%', '80%'],
                offset: '65px',
                title: '查看附件',
                content: $('#viewAttachmentHtml').html(),
                success: function () {
                    if (data.attachment && data.attachment.indexOf(".pdf") !== -1) {
                        PDFObject.embed(data.attachment, "#example1");
                    } else {
                        var attachmentHtml = "";
                        data.attachment.split(",").forEach((attachmentUrl, index) => {
                            attachmentHtml += '<div><div><img src="' + attachmentUrl + '"></div></div>';
                        });
                        $('#example1').html(attachmentHtml);
                    }
                }
            })
        }

        let subjectId = "";

        //初始化客户名称
        admin.req('enterprise/sell/admin/customersInfo/getBasicInfo', {customerType: "2,3"}, function (res) {
            console.log(res.data);
            if (200 == res.code) {

                $("form select[name='customerId']").append(new Option("请选择客户", ""));//往下拉菜单里添加元素
                $.each(res.data, function (index, item) {
                    $("form select[name='customerId']").append(new Option(item.customerName, item.id));//往下拉菜单里添加元素
                })
                $("form select[name='customerId']").val(subjectId);
                form.render(); //菜单渲染 把内容加载进去
            } else {
                layer.msg('获取客户失败!', {icon: 2, time: 1000});
            }
        }, 'GET');

        //产品性质
        admin.getDictList("productNature", "product_nature", null, "产品性质");

        //产品属性
        admin.getDictList("assemblyNature", "product_assembly_nature", null, "产品属性");

        //基础单位
        admin.getDictList("inUnit", "sys_product_detail_base_unit", null, "基础单位");

        // Sku老数据信息
        admin.initDataSelectAndMap("enterprise/product/admin/productDetailWMS/list", {categoryType: "production"}, "inSkuCodeHave", null, "SKU物料", "codeAndName", null, oldSkuInfo);

        //表单提交事件
        form.on('submit(productTaskFormSubmit)', function (d) {
            layer.load(2);
            d.field.customerName = customerName;
            //关联现有的
            d.field.isUseOrAdd = "2";
            d.field.fileName = fileName;
            let productTypeId = $('#productTypeId').val();
            if (productTypeId == null || productTypeId === '') {
                d.field.productTypeId = $("#productTypeId2").val();
            } else {
                d.field.productTypeId = productTypeId;
            }
            d.field.source = 1;

            let listInfo = table.cache['testItemTable'];
            let testItemData = [];
            if (Array.isArray(listInfo) && listInfo.length !== 0) {
                listInfo.forEach(item => {
                    let info = {};
                    info.name = item.name;
                    info.standard = item.standard;
                    info.utensil = item.utensil;
                    info.way = item.way;
                    info.importance = item.importance;
                    info.detectionRate = item.detectionRate;
                    testItemData.push(info);
                })
            }
            d.field.extProductId = $("#extProductId").val();
            d.field.testItemData = testItemData;
            admin.req('enterprise/res/admin/planning/proTask/add', JSON.stringify(d.field), function (res) {
                layer.closeAll('loading');
                if (res.code === 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('productTaskTableInfo');
                    layer.closeAll('page');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST', true, 'application/json; charset=utf-8');
            return false;
        });
    });
</script>